<template>
	<view class="content">

		<view class="top-view">
			<u-field class="input-view" v-if="false" icon="phone" v-model="mobile" label-width="1" placeholder="请输入手机号">
			</u-field>

			<u-field class="input-view" v-if="false" icon="hourglass" v-model="code" label-width="1"
				placeholder="请输入验证码">
				<u-button size="mini" slot="right" type="success">{{codeText}}</u-button>
			</u-field>

			<u-field class="input-view" icon="lock" v-model="oldpwd" label-width="1" password placeholder="请输入旧密码">
			</u-field>
			<u-field class="input-view" icon="lock" v-model="newpwd" label-width="1" password placeholder="请输入新密码">
			</u-field>
			<u-field class="input-view" icon="lock" v-model="confirmpwd" label-width="1" password placeholder="再次输入新密码">
			</u-field>
		</view>

		<button class="login-btn" type="primary" ripple @click="startLogin">确认修改</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile: '',
				code: '',
				newpwd: '',
				codeText: '发送验证码',
				oldpwd: '',
				confirmpwd: ''
			}
		},
		methods: {
			startLogin() {

				if (this.oldpwd.length < 6 || this.newpwd.length < 6 || this.confirmpwd.length < 6) {
					uni.showToast({
						title: '请输入6位或以上密码',
						duration: 2000,
						icon: 'none',
						position: 'bottom'
					});
					return;
				}

				if (this.newpwd != this.confirmpwd) {
					uni.showToast({
						title: '两次密码不一致',
						duration: 2000,
						icon: 'none',
						position: 'bottom'
					});
					return;
				}

				this.requestChange(this.$w_md5.hex_md5_32(this.oldpwd), this.$w_md5.hex_md5_32(this.newpwd));

			},
			requestChange(oldpwd, newpwd) {
				this.$u.post('/client/user/changepwd', {
					oldpwd: oldpwd,
					newpwd: newpwd
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "修改成功",
							duration: 2000,
							icon: 'none',
							position: 'bottom'
						});

						setTimeout(function() {
							uni.navigateBack({
								delta: 2
							});
						}, 1300)


					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none',
							position: 'bottom'
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $uni-bg-color-grey;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 50rpx;

		.top-view {
			background-color: $uni-bg-color;
			width: 100%;
			border-radius: 15rpx;

			.input-view {
				display: flex;
				flex-direction: column;
				justify-content: center;
				width: 100%;
				height: 110rpx;
			}
		}

		.login-btn {
			margin: 40rpx 50rpx 0 50rpx;
			width: 100%;
			font-size: 17px;
		}

	}
</style>
